<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\User;
use app\models\Address;

class AddressController extends Controller
{
    public function actionAdd()
    {
        if(Yii::$app->session['User']['isLogin']!=1){
            return $this->redirect(['member/auth']);
        }
        $usermodel= User::find()->where('username=:name',[':name'=> Yii::$app->session['User']['username']])->one();
        $userid=$usermodel->userid;
        if(Yii::$app->request->isPost){
            $post= Yii::$app->request->post();
            $post['Address']['userid']=$userid;
            $post['Address']['createtime']= time();
            //拼接地址
            $post['Address']['address']=$post['Address']['address'].$post['Address']['addressDetail'];
            unset($post['Address']['addressDetail']);
            $model=new Address();
            $model->load($post);
            $model->save();
        }
        //跳回刚才的页面
        return $this->redirect($_SERVER['HTTP_REFERER']);
    }
    
    public function actionDel()
    {
        if(Yii::$app->session['User']['isLogin']!=1){
            return $this->redirect(['member/auth']);
        }
        $usermodel= User::find()->where('username=:name',[':name'=> Yii::$app->session['User']['username']])->one();
        $userid=$usermodel->userid;
        $addressid= Yii::$app->request->get('addressid');
        if(!Address::find('addressid=:aid and userid=:uid',[':aid'=>$addressid,':uid'=>$userid])->one()){
            return $this->redirect($_SERVER['HTTP_REFERER']);
        }
        Address::deleteAll('addressid=:aid',[':aid'=>$addressid]);
        return $this->redirect($_SERVER['HTTP_REFERER']);
    }
    
}
